package core

import (
	"bytes"
	"log"
	"strconv"
	"strings"
)

func parseNum(cellVal string, buffer *bytes.Buffer) {
	float, e := strconv.ParseFloat(cellVal, 64)
	if e == nil {
		buffer.WriteString(strconv.FormatFloat(float, 'f', -1, 64))
	} else {
		log.Fatal("数字个数错误 num:" + cellVal)
	}
}

func parseBool(cellVal string, buffer *bytes.Buffer) {
	switch strings.ToLower(cellVal) {
	case "1", "true":
		buffer.WriteString("true")
		break
	case "0", "false":
		buffer.WriteString("false")
		break
	default:
		log.Fatal("bool不支持的数据:" + cellVal)
	}
}

//---------------------------------------------tool----------------
func ParseHeadCellVal(v string, headFix [2]string) string {
	ind := strings.Index(v, headFix[0])
	ind2 := strings.Index(v, headFix[1])
	if ind < 0 || ind2 < 0 {
		return ""
	} else {
		headCell := v[ind+1 : ind2]
		return headCell
	}
}

//---
func buildLuaOrJson(row []string, headFix [2]string) *HeadInfo {
	info := new(HeadInfo)
	for k, v := range row {
		cellVal := ParseHeadCellVal(v, headFix)
		if cellVal != "" {
			typeAndName :=
				strings.Split(cellVal, ",")
			tye := typeAndName[0]
			var name string
			if len(typeAndName) > 1 {
				name = typeAndName[1]
			}
			info.AddItem(k, tye, name, v)
		}
	}
	return info
}

//默认配置信息
var defConfig map[string]*DefaultConfig

// defConfig 的Key 要跟Config.ExportConfig 的字段名字一一对应
func GetDefConfig(name string) *DefaultConfig {
	if nil == defConfig {
		defConfig = make(map[string]*DefaultConfig)
		defConfig["LUA"] = NewDefaultConfig(
			[2]string{"(", ")"},
			new(LuaBuilder),
			".lua",
			false,
		)
		defConfig["CSV"] = NewDefaultConfig(
			[2]string{"{", "}"},
			new(CSVBuilder),
			".csv",
			true,
		)
		defConfig["JSON"] = NewDefaultConfig(
			[2]string{"(", ")"},
			new(JSONBuilder),
			".json",
			false,
		)
	}
	return defConfig[name]
}
